import numpy as np
from CoolProp.CoolProp import PropsSI

class FluidProperties:
    def __init__(self):
        self.fluid = 'Water'
        
    def get_properties(self, pressure, temperature, phase='liquid'):
        """计算给定压力和温度下的流体物性
        
        Args:
            pressure: 压力 (Pa)
            temperature: 温度 (K)
            phase: 相态 ('liquid' 或 'vapor')
            
        Returns:
            dict: 包含各种物性的字典
        """
        try:
            if phase == 'liquid':
                density = PropsSI('D', 'P', pressure, 'T', temperature, self.fluid)
                viscosity = PropsSI('V', 'P', pressure, 'T', temperature, self.fluid)
                conductivity = PropsSI('L', 'P', pressure, 'T', temperature, self.fluid)
                cp = PropsSI('C', 'P', pressure, 'T', temperature, self.fluid)
                h = PropsSI('H', 'P', pressure, 'T', temperature, self.fluid)
            else:
                density = PropsSI('D', 'P', pressure, 'T', temperature, self.fluid)
                viscosity = PropsSI('V', 'P', pressure, 'T', temperature, self.fluid)
                conductivity = PropsSI('L', 'P', pressure, 'T', temperature, self.fluid)
                cp = PropsSI('C', 'P', pressure, 'T', temperature, self.fluid)
                h = PropsSI('H', 'P', pressure, 'T', temperature, self.fluid)
                
            return {
                'density': density,
                'viscosity': viscosity,
                'conductivity': conductivity,
                'specific_heat': cp,
                'enthalpy': h
            }
        except Exception as e:
            print(f"Error calculating fluid properties: {e}")
            print(f"Inputs: P={pressure} Pa, T={temperature} K, phase={phase}")
            return None
        
    def get_saturation_properties(self, pressure):
        """计算给定压力下的饱和物性"""
        try:
            T_sat = PropsSI('T', 'P', pressure, 'Q', 0, self.fluid)
            rho_l = PropsSI('D', 'P', pressure, 'Q', 0, self.fluid)
            rho_v = PropsSI('D', 'P', pressure, 'Q', 1, self.fluid)
            h_fg = PropsSI('H', 'P', pressure, 'Q', 1, self.fluid) - PropsSI('H', 'P', pressure, 'Q', 0, self.fluid)
            sigma = PropsSI('I', 'P', pressure, 'Q', 0, self.fluid)  # 表面张力
            
            return {
                'T_sat': T_sat,
                'rho_liquid': rho_l,
                'rho_vapor': rho_v,
                'h_fg': h_fg,
                'surface_tension': sigma
            }
        except Exception as e:
            print(f"Error calculating saturation properties: {e}")
            print(f"Input pressure: {pressure} Pa")
            return None
            
    def check_fluid_state(self, pressure, temperature):
        """检查流体状态（过冷、饱和或过热）"""
        try:
            T_sat = PropsSI('T', 'P', pressure, 'Q', 0, self.fluid)
            
            if temperature < T_sat:
                return 'subcooled'
            elif temperature > T_sat:
                return 'superheated'
            else:
                return 'saturated'
        except Exception as e:
            print(f"Error checking fluid state: {e}")
            print(f"Inputs: P={pressure} Pa, T={temperature} K")
            return None
            
    def get_transport_properties(self, pressure, temperature, phase='liquid'):
        """计算传输物性"""
        try:
            if phase == 'liquid':
                mu = PropsSI('V', 'P', pressure, 'T', temperature, self.fluid)
                k = PropsSI('L', 'P', pressure, 'T', temperature, self.fluid)
                Pr = PropsSI('Prandtl', 'P', pressure, 'T', temperature, self.fluid)
            else:
                mu = PropsSI('V', 'P', pressure, 'T', temperature, self.fluid)
                k = PropsSI('L', 'P', pressure, 'T', temperature, self.fluid)
                Pr = PropsSI('Prandtl', 'P', pressure, 'T', temperature, self.fluid)
                
            return {
                'viscosity': mu,
                'conductivity': k,
                'Prandtl': Pr
            }
        except Exception as e:
            print(f"Error calculating transport properties: {e}")
            print(f"Inputs: P={pressure} Pa, T={temperature} K, phase={phase}")
            return None